# -*- coding=utf-8 -*-

""" 基础篇 """

# import numpy as np
# import pandas as pd
# from scipy.misc import imread, imsave, imresize
# from scipy.spatial.distance import pdist, squareform
# import matplotlib.pyplot as plt
# my_array = np.array([[1, 2, 3, 4, 5],[6, 7, 8, 9, 0]])
# my_new_array = np.zeros((5, 5))
# my_random_array = np.random.random((5))
# my_2d_array_new = np.ones((2, 4))
# my_array_column_2 = my_array[1, :]

# a = np.array([[1.0, 2.0], [3.0, 4.0]])
# b = np.array([[5.0, 6.0], [7.0, 8.0]])
#
# sum = a + b
# difference = a - b
# product = a * b
# quotient = a / b

#算数运算
# print('Sum = \n', sum)
# print('Difference = \n', difference)
# print('Product = \n', product)
# print('Quotient = \n', quotient)

#矩阵运算
# print(a,'\n')
# print(b,'\n')
# matrix_product = a.dot(b)
# print(matrix_product)

# scipy 图像处理
# img = imread('./cat.jpg')
# print(img.dtype, img.shape)
# img_tinted = img*(1, 0.95, 0.9)
#
# plt.subplot(1, 2, 1)
# plt.imshow(img)
#
# plt.subplot(1, 2, 2)
# plt.imshow(np.uint8(img_tinted))
# plt.show()
# img_tinted = imresize(img_tinted, (300, 300))
# imsave('./cat_tinted.jpg', img_tinted)

# scipy 点与点的距离
# x = np.array([[0, 1], [1, 0], [2, 0]])
# print(x)
# d = squareform(pdist(x, 'euclidean'))
# print(d)

# matplotlib 画图
# x = np.arange(0, 3*np.pi, 0.1)
# y_sin = np.sin(x)
# y_cos = np.cos(x)

# plt.plot(x, y_sin)
# plt.plot(x, y_cos)
# plt.xlabel('x axis label')
# plt.ylabel('y axis label')
# plt.title('Sin and Cos')
# plt.legend(['Sin', 'Cos'])
# plt.show()

# plt.subplot(2, 1, 1)
# plt.plot(x, y_sin)
# plt.title('Sin')
#
# plt.subplot(2, 1, 2)
# plt.plot(x, y_cos)
# plt.title('Cos')
# plt.show()
# A = np.array([[2,1,-2],[3,0,1],[1,1,-1]])
# b = np.transpose(np.array([[3,-11,4],[-2,6,0]]))
# print(A)
# print(b)
# x = np.linalg.solve(A, b)
# print(x)

""" 进阶篇 """

import numpy as np

# where替换
# arr = np.arange(10)
# out = np.where(arr % 2 == 1, -1, arr)
# print(out)
# print(arr)

# 垂直、水平叠加
# a = np.arange(10).reshape(2, 5)
# b = np.repeat(1, 10).reshape(2, -1)
# c1 = np.concatenate([a, b], axis=0)
# c0 = np.concatenate([a, b], axis=1)
# print(c1)
# print(c0)
#
# d1 = np.vstack([a, b])
# print(d1)
# print(np.hstack([a, b]))
#
# e1 = np.r_[a, b]
# e0 = np.c_[a, b]
# print(e1)
# print(e0)

# 自定义序列
# a = np.array([1, 2, 3])
# b = np.r_[np.repeat(a, 3), np.tile(a, 3)]
# print(b)

# 获取公共项
# a = np.array([1,2,3,2,3,4,3,4,5,6])
# b = np.array([7,2,10,2,7,4,9,4,9,8])
# c = np.intersect1d(a, b)
# print(c)

# 获取不同项
# a = np.array([1,2,3,4,5])
# b = np.array([5,6,7,8,9])
# c = np.setdiff1d(a, b)
# print(c)

# 获取元素匹配的位置
# a = np.array([1,2,3,2,3,4,3,4,5,6])
# b = np.array([7,2,10,2,7,4,9,4,9,8])
# print(np.where(a == b))

# def maxx(x, y):
#     if x >= y:
#         return x
#     else:
#         return y
#
# pair_max = np.vectorize(maxx, otypes=[float])
#
# a = np.array([5, 7, 9, 8, 6, 4, 5])
# b = np.array([6, 3, 4, 8, 9, 7, 1])
#
# print(pair_max(a, b))

# 交换列行
# arr = np.arange(9).reshape(3,3)
# print(arr[:, [1,0,2]])
# print(arr[[1,0,2], :])

# 反转列行
# arr = np.arange(9).reshape(3,3)
# print(arr[::-1])
# print(arr[:, ::-1])

# np.set_printoptions(threshold=np.nan)
# a = np.arange(15)

# url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
# iris = np.genfromtxt(url, delimiter=',', dtype='object')
# print(iris.shape)
# sepallength = np.genfromtxt(url, delimiter=',', dtype='float', usecols=[0])
# # mu, med, sd = np.mean(sepallength), np.median(sepallength), np.std(sepallength)
# # print(mu, med, sd)
# Smax, Smin = sepallength.max(), sepallength.min()
# S = (sepallength - Smin)/(Smax - Smin)
# S = (sepallength - Smin)/sepallength.ptp()
# print(S)

# np.random.seed(10)
# a = np.random.randint(20, size=10)
# print(a.argsort().argsort())

# from io import BytesIO
# from PIL import Image
# import PIL, requests
#
# URL = 'https://upload.wikimedia.org/wikipedia/commons/8/8b/Denali_Mt_McKinley.jpg'
# response = requests.get(URL)
#
# I = Image.open(BytesIO(response.content))
# I = I.resize([150, 150])
# arr = np.asarray(I)

import numpy as np
import matplotlib.pyplot as plt

def mandlebrot(h, w, maxit=20):
    """ Returns as image of the Mandelbrot fractal of size (h, w) """
    y, x = np.ogrid[-1.4:1.4:h*1j, -2:0.8:w*1j]
    c = x+y*1j
    z = c
    divtime = maxit + np.zeros(z.shape, dtype=int)
    for i in range(maxit):
        z = z**2 + c
        diverge = z*np.conj(z) > 2**2
        div_now = diverge & (divtime==maxit)
        divtime[div_now] = i
        z[diverge] = 2
    return divtime

plt.imshow(mandlebrot(400, 400))
plt.show()


